clear all
set more off, perm
set maxvar 120000
set varabbrev off
* ---------------------------------------------- *
global dir 	"`1'"
global Data 	$dir/Data
global Tables 	$dir/Tables
global Figures 	$dir/Figures
global Work 	$dir/Work
global Temp 	$dir/Temp
* ---------------------------------------------- *
* Add dividend-to-price ratio (msf_adjusted.dta generated by read_crsp.do)
use $Work/msf_adjusted, clear
tsset permno datem
gen  retYr1=1
gen retxYr1=1
forvalues x=0(1)11 {
	replace   retYr1= retYr1*(1+L`x'.ret )
	replace  retxYr1=retxYr1*(1+L`x'.retx)
}
gen dp=retYr1/retxYr1-1
keep permco permno datem dp
gstats winsor dp, cuts(1 99) by(datem) replace  
tempfile dp
save "`dp'", replace
* ************************************************
*compute aggregate LTG
use if LTG<. using $Work/ibes_ret, clear

bys datem: egen Tot=total(mcap)
gen vw=LTG*(mcap/Tot)

collapse (mean) LTGew=LTG (sum) LTG=vw, by(datem)
replace LTG=. if LTG==0

tsset datem
gen    LTGagg=LTG 
gen  L1LTGagg=L12.LTG 
gen d1LLTGagg=LTG-L12.LTG

gen d1FLTGagg=F12.LTG-LTG
gen d3FLTGagg=F36.LTG-LTG
gen d5FLTGagg=F60.LTG-LTG

drop LTG

su LTGagg LTGew

tempfile LTGagg
save "`LTGagg'", replace
* ************************************************
* from read_compustat_qtly.do
use $Work/street_mthly, clear
tsset permno datem
gen L1streetLTM=L12.streetLTM
gen L2streetLTM=L24.streetLTM
gen L3streetLTM=L36.streetLTM
gen L4streetLTM=L48.streetLTM
gen L5streetLTM=L60.streetLTM

keep datem permco permno *LTM
tempfile qtly
save "`qtly'", replace
* ************************************************
use $Work/EBR_new_monthly, clear
merge 1:1 permco permno datem using "`qtly'"
drop if _merge==2
drop _merge

merge m:1 datem using "`LTGagg'"
keep if _merge==3
drop _merge

merge 1:1 permco permno datem using "`dp'"
drop if _merge==2
drop _merge 
* *****************************************************
tempfile Firms01
save "`Firms01'", replace
* *****************************************************
* need STG and most recent CG Revision
use permno permco datem street size op inv sale using $Data/read_compustat, clear
tsset permno datem
tsfill
bys permno: ipolate street datem, gen(streeti)
drop street
replace permco=L.permco if permco==. & permno==L.permno
foreach var of varlist size op inv {
	replace `var'=L.`var' if `var'==. & month(dofm(datem))!=6 & permno==L.permno
}
merge 1:1 permco permno datem using $Work/street_mthly , keepusing(permco permno datem streetLTM)
drop if _merge==2
drop _merge
merge 1:1 permco permno datem using $Work/bemeLTM, keepusing(permco permno datem bemeLTM)
drop if _merge==2
drop _merge
merge 1:1 permco permno datem using $Work/ibes_ret, keepusing(permco permno datem medest* LTG shrout cfacshr)

tsset permno datem
generate EPS=1000*streetLTM/(shrout*cfacshr)
replace  EPS=1000*streeti  /(shrout*cfacshr)	if EPS==.
gen EPS1Yr=F12.EPS 
gen STG2=medest24/medest12 	if medest24>0 & medest12>0

* need more lags of LTG and deltaLTG
forvalues x=1(1)5 {
local j=`x'*12
	gen L`x'LTG=L`j'.LTG
	gen d`x'LLTG=LTG-L`j'.LTG
	gen d`x'FLTG=F`j'.LTG-LTG
}
drop if _merge==2
* *************************************************************
keep permco permno datem STG2 L*LTG d*LTG size bemeLTM op inv sale
* *************************************************************
merge 1:1 permno datem using "`Firms01'"
drop if _merge==1
drop _merge
save $Work/Firms01_June, replace
